\ disasm.part3 of 4 v1.1 NAB
\ Included by disasm.

: .dest
  op >r 9 3 op-bits op>>3
  (hex) 38 and or to op
  ,source r> to op ;

: nib1-3
  12 2 op-bits 4 +cycles case
  1 of 0 endof
  2 of 2 endof
  3 of 1 endof
  abort" illegal move size"
  endcase  to dism-size
  ." move" .size
  .source .dest 4 +mem ;

: dyn$ 4 * s" btstbchgbclrbset" drop
  swap chars + 4 type ;

: .stat-bit
  0 to dism-size 6&7 dyn$ >arg
  .imm ,source ;

: nib0-unique
  3 3 op-bits 1 =
  if ." movep" op>>9 op 6? 1|2
    to dism-size .size 7?
    if .dreg ., .an+w
    else .an+w ,dreg
    then 16 8 +if-long
  else 8?
    if  6&7 dyn$ >arg op>>9 .dreg
      ,source 6 2 +if-long 2 +cycles
    else ." eori" .size .imm ,source
      8 8 +if-long 4 +mem
    then
  then ;

: nib0$
  3 * s" or andsubadd???eorcmp" drop
  swap chars + 3 type ;

: nib0-dest
  9 3 op-bits nib0$
  .size .imm ,source 8 8
  +if-long 4 +mem ;

: nib0
  op case
    (hex) 3c of ." or" .byte bin.# ." ,ccr"
    endof
    (hex) 7c of ." or" .word bin.# ." ,sr"
    endof
    (hex) 23c of ." and" .byte bin.#
      ." ,ccr" endof
    (hex) 27c of ." and" .word bin.#
      ." ,sr" endof
    (hex) a3c of ." eor" .byte bin.#
      ." ,ccr" endof
    (hex) a7c of ." eor" .word bin.#
      ." ,sr" endof
    8 4 op-bits select
      xt nib0-dest xt nib0-unique
      xt nib0-dest xt nib0-unique
      xt nib0-dest xt nib0-dest
      xt nib0-dest xt nib0-unique
      xt .stat-bit xt nib0-unique
      xt nib0-unique xt nib0-unique
      xt nib0-dest xt nib0-unique
      xt nib0-unique xt nib0-unique
    end-select execute drop exit
  endcase  20 +cycles ;

: .cond$
  s" raf hilscccsneeqvcvsplmigeltgtle"
  drop swap 2* chars + 2 type ;

: .cond  8 4 op-bits .cond$ ;

: set-high-branch ( adr -- )
  high-branch max to high-branch ;

: uncond-bra? ( adr -- )
  >r r@ dism-adr - 50 >
  done? dism-done and to dism-done
  r@ start-adr < to dism-done
  r> set-high-branch ;

: cond-bra? ( adr -- ) 8 4 op-bits
  if set-high-branch else uncond-bra?
  then ;

: nib5 6&7?
  if op (binary) 111000 and
    (binary) 1000 =
    if ." db" .cond .word op .dreg
      param dism-adr + cell+ dup
      cond-bra? ., .args-reladr
      a2+ 10 +cycles
    else  ." s" .cond >arg .source
      4 2 +if-long approximate
  then
  else 8? if ." subq"
    else ." addq"
    then
    .size 9 3 op-bits dup 0= if 8 +
    then  .# .num ,source
    4 +long 4 +mem approximate
  then ;

: nib6
  op 255 and 0= if param a2+
    1 to dism-size
  else op byte>cell cell+ 0 to dism-size
  then dism-adr +
  8 4 op-bits 1 = if ." bsr" doing-call
   18 +cycles
  else ." b" .cond dup cond-bra?
   10 +cycles
  then dism-size
  if ." .l" else ." .s" then .args-reladr ;

: .moveq  ." moveq.l" >arg op 255 and
  byte>cell [char] # emit
  .num op>>9 ,dreg 4 +cycles ;

: nib8
  6&7? if 8? ." div"
    if  [char] s 158 +cycles
    else  [char] u 140 +cycles
    then  emit >arg 1 to dism-size
    .source op>>9 ,dreg
  else  op (hex) 1f0 and (hex) 100  =
    if ." sbcd" .byte op>>9 op 3?
      if  .-a@ ., .-a@ 18
      else  .dreg ,dreg  8
      then  +cycles
    else  ." or" .size op>>9 8?
      if .dreg ,source 8 2
      else .source ,dreg 4 2
      then +if-long
    then
  then ;

: nib9
  op>>9 op (hex) c0 and (hex) c0 =
  if ." suba" 8? 1|2 to dism-size .size
    .source ,areg 8 +cycles -2 +mem
    else op (hex) 130 and (hex) 100 =
    8 +cycles approximate
    if  ." subx" .size op 3?
      if  .-a@ ., .-a@ 18 12
      else  .dreg ,dreg  4 4
      then +if-long
    else  ." sub" .size 8? 0=
      if  .source ,dreg 4 2
      else  .dreg ,source 4 2 approximate
      then +if-long
    then
  then ;